package com.example.forum.common.validation;

import com.example.forum.common.AppResult;
import com.example.forum.common.enums.*;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.*;
import lombok.extern.slf4j.Slf4j;

/**
 * @author hanzishuai
 * @date 2025/4/29 13:19
 * @Description 校验工具类
 */
@Slf4j
public class ValidationUtils {

    // 公共校验方法
    /**
     * Created by hanzishuai on 2025/4/29
     *
     * @param id         id
     * @param entityName 校验名
     * @Description 传入 id 和 校验名，校验 id
     */
    public static void id(Long id, String entityName) {
        if (id == null || id <= 0) {
            log.warn("{}：{}，{}", id, entityName, ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
    }


    // 关于数据库相关的校验

    /**
     * Created by hanzishuai on 2025/4/29
     *
     * @param row
     * @Description 校验数据库中受影响的行数是否为  1
     */
    public static void singleRowAffected(int row) {
        if (row != 1) {
            log.warn("{}，预期影响行数：1，实际影响行数：{}", ResultCode.ERROR_SERVICES, row);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    // 关于用户对象的校验

    /**
     * Created by hanzishuai on 2025/5/6
     * @param user   用户对象
     * @Description  校验用户对象是否存在
     */
    public static void exists(User user) {
        if (user == null || user.getDeleteState() == UserStatus.IS_DELETE.getCode()) {
            log.warn("{}，user：{}", ResultCode.FAILED_USER_NOT_EXISTS, maskUser(user));
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
    }

    public static void banned(User user) {
        exists(user);
        if (user.getState() == UserStatus.IS_BANNED.getCode()) {
            log.warn("{}，userId：{}", ResultCode.FAILED_USER_BANNED, user.getId());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
    }

    public static void admin(User user) {
        banned(user);
        if(user.getIsAdmin() == AdminStatus.NOT_ADMIN.getStatus()) {
            log.warn("{}，userId：{}", ResultCode.FAILED_USER_NOT_ADMIN, user.getId());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_ADMIN));
        }

    }

    private static String maskUser(User user) {
        return user == null ? "null" : "User[id=" + user.getId() + "]";
    }

    // 关于帖子对象的校验

    /**
     * Created by hanzishuai on 2025/4/29
     *
     * @param article 帖子对象
     * @Description 校验帖子是否存在
     */
    public static void exists(Article article) {
        if (article == null || article.getDeleteState() == ArticleStatus.IS_DELETE.getStatus()) {
            log.warn("{}，article：{}", ResultCode.FAILED_ARTICLE_NOT_EXISTS, article);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }

    }

    /**
     * Created by hanzishuai on 2025/4/29
     *
     * @param article 帖子对象
     * @return void
     * @Description 判断帖子是否封贴
     */
    public static void banned(Article article) {
        exists(article);
        // 判断帖子是否封贴
        if (article.getState() == ArticleStatus.IS_BANNED.getStatus()) {
            log.warn("{}，articleId：{}", ResultCode.FAILED_ARTICLE_BANNED, article.getId());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
    }

    // 关于帖子回复的校验
    public static void exists(ArticleReply articleReply) {
        if (articleReply == null || articleReply.getDeleteState() == ArticleReplyStatus.IS_DELETE.getStatus()) {
            log.warn("{}，articleReply：{}", ResultCode.FAILED_ARTICLE_REPLY_NOT_EXISTS, articleReply);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_REPLY_NOT_EXISTS));
        }
    }

    public static void banned(ArticleReply articleReply) {
        exists(articleReply);
        // 判断帖子回复是否禁用
        if (articleReply.getState() == ArticleReplyStatus.IS_BANNED.getStatus()) {
            log.warn("{}，articleId：{}", ResultCode.FAILED_ARTICLE_REPLY_BANNED, articleReply.getId());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_REPLY_BANNED));
        }
    }


    // 关于板块对象的校验
    public static void exists(Board board) {
        if (board == null || board.getDeleteState() == BoardStatus.IS_DELETE.getStatus()) {
            log.warn("{}，board：{}", ResultCode.FAILED_BOARD_NOT_EXISTS, board);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
    }

    public static void banned(Board board) {
        exists(board);
        // 判断板块是否封贴
        if (board.getState() == BoardStatus.IS_BANNED.getStatus()) {
            log.warn("{}，boardId：{}", ResultCode.FAILED_BOARD_BANNED, board.getId());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_BANNED));
        }
    }


    // 关于站内信的校验

    public static void exists(Message message) {
        if (message == null || message.getDeleteState() == MessageStatus.IS_DELETE.getStatus()) {
            log.warn("{}，message：{}", ResultCode.FAILED_MESSAGE_NOT_EXISTS, message);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
    }

    public static void banned(Message message) {
        exists(message);
        if (message.getState() == MessageStatus.IS_BANNED.getStatus()) {
            log.warn("{}，messageId：{}", ResultCode.FAILED_MESSAGE_BANNED, message.getId());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_BANNED));
        }
    }



}
